<template>
  <div>
    <h1>商户审核</h1>
    <div>
      <el-form :inline="true" :model="searchData" class="demo-form-inline">
        <el-form-item label="关键词">
          <el-input
            v-model="searchData.keyWord"
            placeholder="请输入关键词"
            clearable
          />
        </el-form-item>
        <el-form-item label="性别">
          <el-select
            v-model="searchData.sex"
            placeholder="请选择性别"
            style="width: 130px"
            clearable
          >
            <el-option label="男" :value="0" />
            <el-option label="女" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-date-picker
            v-model="searchData.beginTime"
            type="datetime"
            placeholder="请选择起始时间"
            value-format="	YYYY-MM-DD HH:mm:ss"
            clearable
          />-
          <el-date-picker
            v-model="searchData.endTime"
            type="datetime"
            placeholder="请选择终止时间"
            value-format="	YYYY-MM-DD HH:mm:ss"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadUerList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="userId" label="用户ID" />
      <el-table-column prop="userName" label="登录账号" />
      <el-table-column prop="nickName" label="真实姓名" />
      <el-table-column prop="sex" label="性别">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.sex == 0">男</el-tag>
          <el-tag type="danger" v-if="scope.row.sex == 1">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="userMobile" label="手机号" width="170px" />
      <el-table-column prop="userStatus" label="状态">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.userStatus == 0"
            >待审核</el-tag
          >
          <el-tag type="success" v-if="scope.row.userStatus == 1">通过</el-tag>
          <el-tag type="danger" v-if="scope.row.userStatus == 2">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="reason" label="原因" />
      <el-table-column prop="createTime" label="注册时间" width="170px" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div style="margin-top: 20px">
    <el-pagination
      v-model:current-page="searchData.pageNum"
      v-model:page-size="searchData.pageSize"
      :page-sizes="[2, 4, 6, 8]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" title="修改商户" width="500">
      <el-form :model="form">
        <el-form-item label="真实姓名" :label-width="formLabelWidth">
          <el-input v-model="form.nickName" disabled autocomplete="off" />
        </el-form-item>
        <el-form-item label="登录账号" :label-width="formLabelWidth">
          <el-input v-model="form.userName" disabled autocomplete="off" />
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth">
          <el-input v-model="form.userMobile" disabled autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.userEmail" disabled autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select
            v-model="form.sex"
            disabled
            placeholder="请选择性别"
            clearable
          >
            <el-option label="男" :value="0" />
            <el-option label="女" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核" :label-width="formLabelWidth">
          <el-switch
            v-model="form.userStatus"
            active-text="通过"
            inactive-text="驳回"
            :active-value="1"
            :inactive-value="2"
          />
        </el-form-item>
        <el-form-item label="驳回原因" :label-width="formLabelWidth">
          <el-input v-model="form.reason" autocomplete="off" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="onSubmitCheck"> 提交 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { getUserList, checkUser } from "@/api/api";
import type { RefSymbol } from "@vue/reactivity";
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted, ref } from "vue";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";

const headers = { token: sessionStorage.getItem("token") };
const tableData = ref([]);
const total = ref(0);
const searchData = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
  sex: null,
  beginTime: null,
  endTime: null,
});
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = ref({
  createTime: null,
  nickName: null,
  reason: null,
  sex: null,
  token: null,
  userAvatar: null,
  userEmail: null,
  userId: null,
  userMobile: null,
  userName: null,
  userPass: null,
  userStatus: null,
});
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchData.value.pageSize = val;
  loadUerList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchData.value.pageNum = val;
  loadUerList();
};

const handleEdit = (row: any) => {
  console.log(row);
  Object.assign(form.value, row);
  dialogFormVisible.value = true;
};
const onSubmitCheck = () => {
  console.log("onSubmitCheck", form.value);
  if (form.value.userStatus == 2) {
    if (form.value.reason == null || form.value.reason == "") {
      ElMessage.error("请填写驳回原因");
      return;
    }
  }
  checkUser(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("审核成功");
      dialogFormVisible.value = false;
      loadUerList();
    } else {
      ElMessage.error("审核失败");
    }
  });
};
const loadUerList = () => {
  getUserList(searchData.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadUerList();
});
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
